﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Model;
using V5_Common;
using V5_BLL;
using System.Text;

namespace V5_EShop._Template.Default
{
    public partial class product_details : BasicPage
    {
        protected ModelProduct product = null;
        public ModelUser m_user = new ModelUser();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                int pid = Input.SetInt(Request.QueryString["pid"]);
                if (pid > 0)
                {
                    product = new BLLProduct().GetModel(pid, true);
                    //更新 点击次数
                    product.Hits = product.Hits + 1;
                    new BLLProduct().Update(product);
                    //绑定客户能选择的字段
                    Bind_CustomChoice();
                    //绑定产品多图
                    Bind_ProductMorePic();
                    //绑定产品字段
                    Bind_ProductPro();
                    //绑定品论
                    Bind_ProductComment();
                    //绑定问答
                    Bind_ProductQuestionAndAsk();
                }
            }
        }
        //绑定多图
        private void Bind_ProductMorePic()
        {
            Repeater rp = (Repeater)this.FindControl("rpProductMorePic");
            if (rp != null)
            {
                DataTable dt = new BLLProduct().Get_ProductMorePics(product.ProductMorePic);
                if (dt != null)
                {
                    rp.DataSource = dt.DefaultView;
                    rp.DataBind();
                }
            }
        }

        //绑定可选属性
        private void Bind_CustomChoice()
        {
            DataTable customChoice = product.Choicetable;
            if (customChoice != null)
            {
                Repeater rp = (Repeater)this.FindControl("rpProductFieldSelect");
                if (rp != null)
                {
                    DataView dv = customChoice.DefaultView;
                    rp.DataSource = dv;
                    rp.DataBind();
                }
            }
        }

        //获取客户端
        protected string Get_CustomChoice(int indexid, int filedid, string FieldValue)
        {
            ModelProductField modelproductfield = new BLLProductField().GetModel(filedid);
            if (modelproductfield.IsUseImg == 1)//判断是否用户可以选择
            {
                string fieldName = modelproductfield.FieldName;
                string fieldvalues = modelproductfield.FieldValues;
                string fieldpics = modelproductfield.FieldPics;

                string[] fieldvalueArr = fieldvalues.Split(new char[] { ',' });
                string[] filedpicArr = fieldpics.Split(new char[] { ',' });

                string[] s1 = FieldValue.Split(new char[] { ',' });

                StringBuilder sb = new StringBuilder();
                sb.Append("<li>");
                sb.Append(@"<span id=""C_custom_" + indexid + @""" class=""hong""><span class=""hui"">" + fieldName + @"：</span><span >" + s1[0] + "</span> </span>");
                sb.Append(@"<span class=""mianliao"">");


                for (int n = 0; n < s1.Length; n++)
                {
                    for (int i = 0; i < fieldvalueArr.Length; i++)
                    {
                        for (int j = 0; j < filedpicArr.Length; j++)
                        {
                            if (i == j && s1[n] == fieldvalueArr[i])
                            {
                                sb.Append(@"<img src=""UpLoadFiles/productFiled/" + filedpicArr[j] + @""" onclick=""changeCustom('custom_" + indexid + @"','" + fieldName + @"：" + s1[n] + @"')"">");
                            }
                        }
                    }
                }
                sb.Append(@"</span>");
                sb.Append(@"<div class=""clear""></div></li>");
                return sb.ToString();
            }
            return "";
        }
        //绑定其他字段属性
        protected void Bind_ProductPro()
        {
            Repeater rp = (Repeater)this.FindControl("rpListProductPro");
            if (rp != null)
            {
                DataTable dt = new DataTable();

                dt.Columns.Add(new DataColumn("m1"));
                dt.Columns.Add(new DataColumn("m2"));
                if (product.ProductSingleFields.Trim().Length > 0)
                {
                    string[] sarr1 = product.ProductSingleFields.Split(new string[] { "||" }, StringSplitOptions.None);
                    foreach (string s1 in sarr1)
                    {
                        string[] sarr2 = s1.Split(new string[] { ":" }, StringSplitOptions.None);
                        DataRow dr = dt.NewRow();
                        dr["m1"] = sarr2[0];
                        dr["m2"] = sarr2[1];
                        dt.Rows.Add(dr);
                    }
                }
                rp.DataSource = dt;
                rp.DataBind();
            }
        }
        public int CommentCount = 0;
        private void Bind_ProductComment()
        {

            string strWhere = "";
            strWhere = " ProductID=" + Utils.FilterInjection(product.ID) + " and Passed=1 ";

            DataTable dt = new BLLProductComment().GetList(0, strWhere, " ID Desc").Tables[0];
            CommentCount = dt.Rows.Count;
            this.rpProductCommentList.DataSource = dt;
            this.rpProductCommentList.DataBind();
        }

        protected string Get_SysAnswer(string replyComment)
        {
            if (replyComment != "" && replyComment.Length > 0)
            {
                return @"<div class=""answers"">
                                    <strong>管理员 回答：</strong> <span style=""display: block; margin: 10px 0 0 0px; text-indent: 24px;"">" + replyComment + @"</span>
                                </div>";
            }
            return "";
        }

        /// <summary>
        /// 绑定洗涤
        /// </summary>
        /// <param name="xdstrings"></param>
        /// <returns></returns>
        public string Get_ProductXJ(string xdstrings, int _width, int _height)
        {
            string tempstr = "";
            if (xdstrings != "" && xdstrings.Length > 0)
            {
                foreach (string s1 in xdstrings.Split(new string[] { "," }, StringSplitOptions.None))
                {
                    tempstr += "<img src='/images/xd/xd_" + s1 + ".jpg' width='" + _width + "' height='" + _height + "' alt='洗涤'/>";
                }
            }
            return tempstr;
        }
        #region 绑定问答
        public int QuestionCount = 0;
        //绑定提问
        private void Bind_ProductQuestionAndAsk()
        {
            Repeater rp = (Repeater)this.FindControl("rpQuestionList");
            if (rp != null)
            {
                string strWhere = "";
                strWhere = " ProductID=" + product.ID + " and Passed=1 ";
                DataTable dt = new BLLProductAsk().GetList(2, strWhere, " ID Desc ").Tables[0];
                QuestionCount = dt.Rows.Count;
                rp.DataSource = dt;
                rp.DataBind();
                ask_answer_count = dt.Rows.Count;
            }
        }
        /// <summary>
        /// 绑定回答
        /// </summary>
        /// <param name="questionid"></param>
        /// <returns></returns>
        public DataTable Bind_ProductAsk(object questionid)
        {
            return new BLLProductAnswer().GetList(5, " ID=" + Utils.FilterInjection(questionid) + " and passed=1 ", " ID desc ").Tables[0];
        }
        public int ask_answer_count = 0;

        #endregion

        /// <summary>
        /// 绑定导航
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public string BindClass(int classID)
        {
            ModelProductClass model1 = new BLLProductClass().GetModel(classID);
            int parentID1 = model1.ParentID;
            ModelProductClass model2 = null;
            return "";
        }
    }
}
